
var ${materialDetail.material.domainName?uncap_first} = {
	
	URL_LIST: 			contextPath + '/${materialDetail.material.domainName?uncap_first}/ajaxList',
	URL_CREATE: 		contextPath + '/${materialDetail.material.domainName?uncap_first}/ajaxCreate',
	URL_UPDATE: 		contextPath + '/${materialDetail.material.domainName?uncap_first}/ajaxUpdate',
	URL_BATCH_UPDATE: 	contextPath + '/${materialDetail.material.domainName?uncap_first}/ajaxBatchUpdate',
	URL_REMOVE: 		contextPath + '/${materialDetail.material.domainName?uncap_first}/ajaxRemove',
	URL_REPORT: 		contextPath + '/${materialDetail.material.domainName?uncap_first}/report',
	PK_FIELD: 			'${materialDetail.pk.propertyName}',
	
	init: function() {
		${materialDetail.material.domainName?uncap_first}.initList();
		
		var searchItems = [
			<#list materialDetail.pcMappings as pcMapping> 
			<#if !pcMapping.isPk()> 
			{key: '${pcMapping.propertyName}', 			label: '${pcMapping.columnRemarks!'未指定'}'},
			</#if>
			</#list>
		];
		application.initSearchForm(searchItems, '#${materialDetail.material.domainName?uncap_first}-search');
	},
	
	initList: function() {
		
	    var columns = [[  
	        <#list materialDetail.pcMappings as pcMapping> 
	    	<#if pcMapping.isPk()> 
	        {field: '${pcMapping.propertyName}', checkbox: true},  
	        <#else>
	        {field: '${pcMapping.propertyName}', title: '${pcMapping.columnRemarks!'未指定'}', width: 100, align: 'center', formatter: function(value, row) { return value; }},  
	        </#if>
	        </#list> 
	    ]];
	            
		$('#${materialDetail.material.domainName?uncap_first}-datagrid').datagrid({  
		    url: ${materialDetail.material.domainName?uncap_first}.URL_LIST,  
		    columns: columns,
		    pageList: [30, 50, 100],
		});  
		
	},
	
	initForm: function() {
		$('textarea.ckeditor', '#${materialDetail.material.domainName?uncap_first}-form').ckeditor();
	},
	
	search: function() {
		var data = application.getSearchData($('#${materialDetail.material.domainName?uncap_first}-search'));
		$('#${materialDetail.material.domainName?uncap_first}-datagrid').datagrid('load', data);
	},
	
	create: function() {
		$('#${materialDetail.material.domainName?uncap_first}-form-dialog').dialog('open').dialog('setTitle', '新建');
		${materialDetail.material.domainName?uncap_first}.initForm();
		application.clearForm('#${materialDetail.material.domainName?uncap_first}-form');
		${materialDetail.material.domainName?uncap_first}.formActionURL = ${materialDetail.material.domainName?uncap_first}.URL_CREATE;
	},
	
	update: function() {
		var row = $('#${materialDetail.material.domainName?uncap_first}-datagrid').datagrid('getSelected');
		if (row){
			$('#${materialDetail.material.domainName?uncap_first}-form-dialog').dialog('open').dialog('setTitle', '修改');
			${materialDetail.material.domainName?uncap_first}.initForm();
			$.getJSON(${materialDetail.material.domainName?uncap_first}.URL_UPDATE, {pk: row[${materialDetail.material.domainName?uncap_first}['PK_FIELD']]}, function(responseData) {
				application.loadForm('#${materialDetail.material.domainName?uncap_first}-form', responseData);
				${materialDetail.material.domainName?uncap_first}.formActionURL = ${materialDetail.material.domainName?uncap_first}.URL_UPDATE;
			});
		}
	},
	
	save: function() {
		$('#${materialDetail.material.domainName?uncap_first}-form').form('submit', {
			url: ${materialDetail.material.domainName?uncap_first}.formActionURL,
			onSubmit: function(){
				for ( instance in CKEDITOR.instances ) {
  					CKEDITOR.instances[instance].updateElement();
				}
				return $(this).form('validate');
			},
			success: ${materialDetail.material.domainName?uncap_first}.postResultHandler
		});
	},
	
	postResultHandler: function(result) {
		var result = eval('(' + result + ')');
		if (!result.success){
			$.messager.show({title: '发生错误', msg: result.message});
		} else {
			$('#${materialDetail.material.domainName?uncap_first}-form-dialog').dialog('close'); 
			$('#${materialDetail.material.domainName?uncap_first}-datagrid').datagrid('reload'); 
		}
	},
	
	batchUpdate: function() {
		var rows = $('#${materialDetail.material.domainName?uncap_first}-datagrid').datagrid('getSelections');
		var selectedPks = application.parseSelectedFields(rows, ${materialDetail.material.domainName?uncap_first}['PK_FIELD']);
		if (selectedPks){
			$.messager.confirm('确认信息', '确定要批量修改已选数据吗？', function(result){
				if (result) ${materialDetail.material.domainName?uncap_first}.doBatchUpdate(selectedPks);
			});
		}
	},
	
	doBatchUpdate: function(selectedPks) {
		var field = $('#${materialDetail.material.domainName?uncap_first}-batch-update').val();
		var fieldValue = $('#${materialDetail.material.domainName?uncap_first}-batch-update-value').val();
		$.post(
			${materialDetail.material.domainName?uncap_first}.URL_BATCH_UPDATE, 
			{pks: selectedPks, field: field, fieldValue: fieldValue}, 
			${materialDetail.material.domainName?uncap_first}.postResultHandler);
	},
	
	remove: function() {
		var rows = $('#${materialDetail.material.domainName?uncap_first}-datagrid').datagrid('getSelections');
		var selectedPks = application.parseSelectedFields(rows, ${materialDetail.material.domainName?uncap_first}['PK_FIELD']);
				
		if (selectedPks){
			$.messager.confirm('确认信息', '确定要删除已选数据吗？', function(result){
				if (result) ${materialDetail.material.domainName?uncap_first}.doRemove(selectedPks);
			});
		}
	},
	
	doRemove: function(selectedPks) {
		$.post(${materialDetail.material.domainName?uncap_first}.URL_REMOVE, {pks: selectedPks}, ${materialDetail.material.domainName?uncap_first}.postResultHandler);
	},
	
	report: function() {
		var params = application.getSearchParams;
		window.location = ${materialDetail.material.domainName?uncap_first}.URL_REPORT + '?' + params;
	},
	

};

${materialDetail.material.domainName?uncap_first}.init();
